#include "signin.h"
#include "ui_signin.h"

SignIn::SignIn(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SignIn)
{
    ui->setupUi(this);
    this->setWindowTitle("OpenChatRoom");
    this->setFixedSize(850,500);
    QPalette PAllbackground = this->palette();
    QImage ImgAllbackground(":/source/sigin.jpg");
    QImage fitimgpic=ImgAllbackground.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
    PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
    this->setPalette(PAllbackground);

    connect(ui->backButton,&QPushButton::clicked,this,[=](){
        back();
        ui->uidLineEdit->clear();
        ui->passwordLineEdit->clear();
        ui->nameLineEdit->clear();
        ui->questionLineEdit->clear();
        ui->answerLineEdit->clear();
        });


//    db = QSqlDatabase::addDatabase("QMYSQL");
//    db.setHostName("47.115.215.143");
//    db.setPort(3307);
//    db.setDatabaseName("YUNMYSQL");//数据库名字
//    db.setUserName("root");//账号
//    db.setPassword("123456");//密码
//    db.open();



}

SignIn::~SignIn()
{
    //db.close();

    delete ui;
}

void SignIn::on_siginInButton_clicked()
{
    //获取uid，passwor,密保问题及答案
    QString uid=ui->uidLineEdit->text();
    QString password=ui->passwordLineEdit->text();
    QString name=ui->nameLineEdit->text();
    QString question=ui->questionLineEdit->text();
    QString answer=ui->answerLineEdit->text();
    if(uid==""||password==""||name==""||question==""||answer=="")
    {
        QMessageBox::warning(this,"警告","信息未填完整");
    }
    else
    {
        QString sql=QString("select Uid from User where Uid='%1' ").arg(uid);
        QSqlQuery query;

        if(query.exec(sql))
        {
            if(query.next()&&QString::compare(uid,query.value(0).toString())==0)
            {
                QMessageBox::warning(this,"警告","uid已存在");
            }
            else
            {
                sql=QString("insert into User values('%1','%2','%3','%4','%5');").arg(uid).arg(password).arg(name).arg(question).arg(answer);
                query.exec(sql);
                QMessageBox::information(this,"提示","注册成功");
            }
        }
    }


}

